package com.army.study.utils;

import com.army.study.database.Bus;
import com.army.study.database.BusComesDatabase;
import com.army.study.database.Form;
import com.army.study.database.Station;
import ohos.data.orm.OrmContext;
import ohos.data.orm.OrmPredicates;

import java.util.ArrayList;
import java.util.List;

public class DatabaseUtils {

    /**
     * 新增卡片信息
     *
     * @param form 卡片对象
     * @param connect 数据连接
     */
    public static void insertForm(OrmContext connect, Form form) {
        connect.insert(form);
        connect.flush();
    }
    /**
     * 更新卡片信息
     *
     * @param form 卡片对象
     * @param connect 数据连接
     */
    public static void updateForm(OrmContext connect, Form form) {
        connect.update(form);
        connect.flush();
    }
    /**
     * 获取Form数据
     *
     * @param formId 卡片id
     * @param connect 数据连接
     */
    public static Form getFormData(OrmContext connect, long formId) {
        OrmPredicates where = connect.where(Form.class);
        where.equalTo("formId", formId);
        List<Form> forms = connect.query(where);
        if (!forms.isEmpty()) {
            return forms.get(0);
        }
        return null;
    }
    /**
     * 删除数据
     *
     * @param formId 卡片id
     * @param connect 数据连接
     */
    public static void deleteFormData(OrmContext connect, long formId) {
        OrmPredicates where = connect.where(Form.class);
        where.equalTo("formId", formId);
        List<Form> forms = connect.query(where);
        if (!forms.isEmpty()) {
            connect.delete(forms.get(0));
            connect.flush();
        }
    }

    /**
     * 根据Id,搜索出公交车信息
     *
     * @param connect
     * @param busId
     * @return
     */
    public static Bus getBusById(OrmContext connect, Long busId) {
        OrmPredicates where = new OrmPredicates(Bus.class);
        where.equalTo("id", busId);
        List<Bus> busList = connect.query(where);
        if (!busList.isEmpty()) {
            return busList.get(0);
        }
        return null;
    }

    /**
     * 根据关键字,搜索出公交车信息
     *
     * @param connect
     * @param keyword
     * @return
     */
    public static List<Bus> getBubsByKeyword(OrmContext connect, String keyword) {
        OrmPredicates ormPredicates = new OrmPredicates(Bus.class);
        if (!keyword.isEmpty()) {
            ormPredicates.contains("busNum", keyword);
        }
        return connect.query(ormPredicates);
    }

    /**
     * 根据公交车Id, 查找出公交行驶的车站牌信息
     *
     * @param connect
     * @param parentId
     * @return
     */
    public static List<Station> getStationByParentId(OrmContext connect, Long parentId) {
        OrmPredicates ormPredicates = new OrmPredicates(Station.class);
        ormPredicates.equalTo("parentId", parentId);

        return connect.query(ormPredicates);
    }

    /**
     * 保存公交车信息
     *
     * @param connect
     * @param bus
     */
    public static void insertBusData(OrmContext connect, Bus bus) {
        connect.insert(bus);
        connect.flush();
    }

    /**
     * 批量保存公交车信息
     *
     * @param connect
     */
    public static void insertList(OrmContext connect) {
        List<Bus> busList = getBusData();
        for (Bus bus : busList) {
            try {
                connect.insert(bus);
                boolean result = connect.flush();
                if (result) {
                    List<Station> stationList = getStationData(bus.getId());
                    insertStationList(connect, stationList);
                }
            }catch (Exception e) {
                System.out.println(bus.getBusNum() + " 公交车已经存在, 忽略...");
            }
        }
    }

    /**
     * 批量保存车站牌信息
     *
     * @param connect
     * @param stationList
     */
    public static void insertStationList(OrmContext connect, List<Station> stationList) {
        // 同步批量保存车站牌信息
        for (Station station : stationList) {
            try {
                connect.insert(station);
                connect.flush();
                System.out.println("插入车牌路线是: " + station.getParentName() + ", 站牌是: " + station.getStationName());
            }catch (Exception e) {
                System.out.println(station.getStationName() + " 站牌已经存在, 忽略...");
            }
        }

//        // 异步批量保存车站牌信息
//        new AsyncInitStation(connect, stationList).start();
    }

    /**
     * 多线程异步保存车站牌信息
     */
    static class AsyncInitStation extends Thread {
        private OrmContext connect;
        private List<Station> stationList;

        public AsyncInitStation(OrmContext connect, List<Station> stationList) {
            this.connect = connect;
            this.stationList = stationList;
        }

        @Override
        public void run() {
            System.out.println("开始异步插入车站牌信息...");
            for (Station station : stationList) {
                System.out.println("插入车牌路线是: " + station.getParentName() + ", 站牌是: " + station.getStationName());
                connect.insert(station);
                connect.flush();
            }
            System.out.println("结束异步插入车站牌信息...");
        }
    }

    /**
     * 清空公交车信息
     *
     * @param connect
     */
    public static void deleteAllBusData(OrmContext connect) {
        List<Station> stations = connect.query(new OrmPredicates(Station.class));
        for (Station station : stations) {
            connect.delete(station);
            connect.flush();
        }

        List<Bus> buses = connect.query(new OrmPredicates(Bus.class));
        for (Bus bus : buses) {
            connect.delete(bus);
            connect.flush();
        }
    }

    public static List<Bus> getBusData() {
        List<Bus> busList = new ArrayList<>();

        Bus bus1 = new Bus(1L, "987路", "海珠客运站", "天安科技园", 3, 1, 0.5f);
        busList.add(bus1);
        Bus bus2 = new Bus(2L, "303路", "沙园", "海珠客运站总站", 10, 2, 1.5f);
        busList.add(bus2);
        Bus bus3 = new Bus(3L, "288路", "钟村市场", "洛溪新城总站", 15, 4, 2.5f);
        busList.add(bus3);
        Bus bus4 = new Bus(4L, "129路", "锦绣香江", "奥林匹克花园总站", 10, 2, 1);
        busList.add(bus4);
        Bus bus5 = new Bus(5L, "230路", "石溪站", "天河电脑城", 20, 6, 4.5f);
        busList.add(bus5);
        Bus bus6 = new Bus(6L, "882路", "洛溪桥脚", "员村站", 30, 9, 7);
        busList.add(bus6);
        Bus bus7 = new Bus(7L, "551路", "宝岗大道南", "体育中心", 10, 1, 0.5f);
        busList.add(bus7);
        Bus bus8 = new Bus(8L, "86路", "三滘村", "广州大学城", 25, 7, 6.5f);
        busList.add(bus8);

        return busList;
    }

    /**
     * 初始化站牌信息
     * @param parentId
     * @return
     */
    public static List<Station> getStationData(Long parentId) {
        List<Station> stationList = new ArrayList<>();

        Station station1 = new Station("海珠客运站总站", 1L, "987路", "海珠客运站总站", "天安科技园总站", 1);
        stationList.add(station1);
        Station station2 = new Station("三滘村", 1L, "987路", "海珠客运站总站", "天安科技园总站",  2);
        stationList.add(station2);
        Station station3 = new Station("沥滘", 1L, "987路", "海珠客运站总站", "天安科技园总站",  3);
        stationList.add(station3);
        Station station4 = new Station("后滘村", 1L, "987路", "海珠客运站总站", "天安科技园总站",  4);
        stationList.add(station4);
        Station station5 = new Station("沙溪村", 1L, "987路", "海珠客运站总站", "天安科技园总站",  5);
        stationList.add(station5);
        Station station6 = new Station("星河湾", 1L, "987路", "海珠客运站总站", "天安科技园总站",  6);
        stationList.add(station6);
        Station station7 = new Station("塘步西", 1L, "987路", "海珠客运站总站", "天安科技园总站",  7);
        stationList.add(station7);
        Station station8 = new Station("南大路口", 1L, "987路", "海珠客运站总站", "天安科技园总站",  8);
        stationList.add(station8);
        Station station9 = new Station("华南碧桂园", 1L, "987路", "海珠客运站总站", "天安科技园总站",  9);
        stationList.add(station9);
        Station station10 = new Station("里仁洞", 1L, "987路", "海珠客运站总站", "天安科技园总站",  10);
        stationList.add(station10);
        Station station11 = new Station("冼庄立交", 1L, "987路", "海珠客运站总站", "天安科技园总站",  11);
        stationList.add(station11);
        Station station12 = new Station("万博翠湖花园", 1L, "987路", "海珠客运站总站", "天安科技园总站",  12);
        stationList.add(station12);
        Station station13 = new Station("天安科技园总站", 1L, "987路", "海珠客运站总站", "天安科技园总站",  13);
        stationList.add(station13);

        Station station21 = new Station("太古仓路总站", 2L, "303路", "太古仓路总站", "海珠客运站总站", 1);
        stationList.add(station21);
        Station station22 = new Station("革新路", 2L, "303路", "太古仓路总站", "海珠客运站总站", 2);
        stationList.add(station22);
        Station station23 = new Station("光大花园", 2L, "303路", "太古仓路总站", "海珠客运站总站", 3);
        stationList.add(station23);
        Station station24 = new Station("太古仓", 2L, "303路", "太古仓路总站", "海珠客运站总站", 4);
        stationList.add(station24);
        Station station25 = new Station("金沙路", 2L, "303路", "太古仓路总站", "海珠客运站总站", 5);
        stationList.add(station25);
        Station station26 = new Station("凤凰新村", 2L, "303路", "太古仓路总站", "海珠客运站总站", 6);
        stationList.add(station26);
        Station station27 = new Station("沙园", 2L, "303路", "太古仓路总站", "海珠客运站总站", 7);
        stationList.add(station27);
        Station station28 = new Station("基建新村", 2L, "303路", "太古仓路总站", "海珠客运站总站", 8);
        stationList.add(station28);
        Station station29 = new Station("珠江医院", 2L, "303路", "太古仓路总站", "海珠客运站总站", 9);
        stationList.add(station29);
        Station station210 = new Station("纸厂", 2L, "303路", "太古仓路总站", "海珠客运站总站", 10);
        stationList.add(station210);
        Station station211 = new Station("保利花园", 2L, "303路", "太古仓路总站", "海珠客运站总站", 11);
        stationList.add(station211);
        Station station212 = new Station("石溪", 2L, "303路", "太古仓路总站", "海珠客运站总站", 12);
        stationList.add(station212);
        Station station213 = new Station("新滘西路口", 2L, "303路", "太古仓路总站", "海珠客运站总站", 13);
        stationList.add(station213);
        Station station214 = new Station("金碧花园", 2L, "303路", "太古仓路总站", "海珠客运站总站", 14);
        stationList.add(station214);
        Station station215 = new Station("南洲名苑", 2L, "303路", "太古仓路总站", "海珠客运站总站", 15);
        stationList.add(station215);
        Station station216 = new Station("海珠客运站总站", 2L, "303路", "太古仓路总站", "海珠客运站总站", 16);
        stationList.add(station216);

        Station station31 = new Station("大夫山公园总站", 3L, "288路", "大夫山公园总站", "洛溪新城总站", 1);
        stationList.add(station31);
        Station station32 = new Station("锦秀生态园", 3L, "288路", "大夫山公园总站", "洛溪新城总站", 2);
        stationList.add(station32);
        Station station33 = new Station("钟村市场", 3L, "288路", "大夫山公园总站", "洛溪新城总站", 3);
        stationList.add(station33);
        Station station34 = new Station("锦秀文化中心", 3L, "288路", "大夫山公园总站", "洛溪新城总站", 4);
        stationList.add(station34);
        Station station35 = new Station("谢村", 3L, "288路", "大夫山公园总站", "洛溪新城总站", 5);
        stationList.add(station35);
        Station station36 = new Station("石壁路口", 3L, "288路", "大夫山公园总站", "洛溪新城总站", 6);
        stationList.add(station36);
        Station station37 = new Station("万宝工业基地", 3L, "288路", "大夫山公园总站", "洛溪新城总站", 7);
        stationList.add(station37);
        Station station38 = new Station("飘峰", 3L, "288路", "大夫山公园总站", "洛溪新城总站", 8);
        stationList.add(station38);
        Station station39 = new Station("猛涌", 3L, "288路", "大夫山公园总站", "洛溪新城总站", 9);
        stationList.add(station39);
        Station station310 = new Station("香江动物园", 3L, "288路", "大夫山公园总站", "洛溪新城总站", 10);
        stationList.add(station310);
        Station station311 = new Station("礼村", 3L, "288路", "大夫山公园总站", "洛溪新城总站", 11);
        stationList.add(station311);
        Station station312 = new Station("大石", 3L, "288路", "大夫山公园总站", "洛溪新城总站", 12);
        stationList.add(station312);
        Station station313 = new Station("大石桥南", 3L, "288路", "大夫山公园总站", "洛溪新城总站", 13);
        stationList.add(station313);
        Station station314 = new Station("上漖", 3L, "288路", "大夫山公园总站", "洛溪新城总站", 14);
        stationList.add(station314);
        Station station315 = new Station("洛溪新城总站", 3L, "288路", "大夫山公园总站", "洛溪新城总站", 15);
        stationList.add(station315);

        Station station41 = new Station("洛溪新城总站", 4L, "129路", "洛溪新城总站", "奥林匹克花园总站", 1);
        stationList.add(station41);
        Station station42 = new Station("上漖", 4L, "129路", "洛溪新城总站", "奥林匹克花园总站", 2);
        stationList.add(station42);
        Station station43 = new Station("大石桥北", 4L, "129路", "洛溪新城总站", "奥林匹克花园总站", 3);
        stationList.add(station43);
        Station station44 = new Station("五洲装饰城", 4L, "129路", "洛溪新城总站", "奥林匹克花园总站", 4);
        stationList.add(station44);
        Station station45 = new Station("沙溪村", 4L, "129路", "洛溪新城总站", "奥林匹克花园总站", 5);
        stationList.add(station45);
        Station station46 = new Station("星河湾", 4L, "129路", "洛溪新城总站", "奥林匹克花园总站", 6);
        stationList.add(station46);
        Station station47 = new Station("塘步西", 4L, "129路", "洛溪新城总站", "奥林匹克花园总站", 7);
        stationList.add(station47);
        Station station48 = new Station("南大路口", 4L, "129路", "洛溪新城总站", "奥林匹克花园总站", 8);
        stationList.add(station48);
        Station station49 = new Station("万博中心", 4L, "129路", "洛溪新城总站", "奥林匹克花园总站", 9);
        stationList.add(station49);
        Station station410 = new Station("锦绣香江", 4L, "129路", "洛溪新城总站", "奥林匹克花园总站", 10);
        stationList.add(station410);
        Station station411 = new Station("锦绣香江北门", 4L, "129路", "洛溪新城总站", "奥林匹克花园总站", 11);
        stationList.add(station411);
        Station station412 = new Station("地铁汉溪长隆C站", 4L, "129路", "洛溪新城总站", "奥林匹克花园总站", 12);
        stationList.add(station412);
        Station station413 = new Station("奥林匹克花园总站", 4L, "129路", "洛溪新城总站", "奥林匹克花园总站", 13);
        stationList.add(station413);





        List<Station> filter = new ArrayList<>();
        for (Station stt : stationList) {
            if (stt.getParentId() == parentId) {
                filter.add(stt);
            }
        }
        return filter;
    }
}
